import { defineStore } from 'pinia';
import { login, register } from '@/service';
import localCache from '@/utils/cache';
const useUserStore = defineStore('user', {
  state: () => ({
    token: localCache.getCache('token') ?? '',
    isLogin: localCache.getCache('isLogin') ?? false,
    userInfo: localCache.getCache('userInfo') ?? '',
  }),
  actions: {
    async userLogin(data) {
      const res = await login(data);
      //存入token
      localCache.setCache('token', res.result.token);
      this.token = res.result.token;
      //设置登录态
      localCache.setCache('isLogin', true);
      this.isLogin = true;
      //存入用户信息
      localCache.setCache('userInfo', res.result);
      this.userInfo = res.result;
      // this.isLogin = true;
    },
    async userRegister(data) {
      const res = await register(data);
    },
  },
});
export default useUserStore;
